package com.ktjy.ktmall.controller;

import com.ktjy.common.annotation.Log;
import com.ktjy.common.core.controller.BaseController;
import com.ktjy.common.core.domain.AjaxResult;
import com.ktjy.common.enums.BusinessType;
import com.ktjy.common.utils.poi.ExcelUtil;
import com.ktjy.ktmall.domain.GoodsCategory;
import com.ktjy.ktmall.service.IGoodsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 分类菜单Controller
 *
 * @author ktjy
 * @date 2025-01-14
 */
@RestController
@RequestMapping("/ktmall/category")
public class GoodsCategoryController extends BaseController
{
    @Autowired
    private IGoodsCategoryService goodsCategoryService;

/**
 * 查询分类菜单列表
 */
@PreAuthorize("@ss.hasPermi('ktmall:category:list')")
@GetMapping("/list")
        public AjaxResult list(GoodsCategory goodsCategory)
        {
            List<GoodsCategory> list = goodsCategoryService.selectGoodsCategoryList(goodsCategory);
            return success(list);
        }


    /**
     * 导出分类菜单列表
     */
    @PreAuthorize("@ss.hasPermi('ktmall:category:export')")
    @Log(title = "分类菜单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GoodsCategory goodsCategory)
    {
        List<GoodsCategory> list = goodsCategoryService.list();
        ExcelUtil<GoodsCategory> util = new ExcelUtil<GoodsCategory>(GoodsCategory.class);
        util.exportExcel(response, list, "分类菜单数据");
    }

    /**
     * 获取分类菜单详细信息
     */
    @PreAuthorize("@ss.hasPermi('ktmall:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(goodsCategoryService.getById(id));
    }

    /**
     * 新增分类菜单
     */
    @PreAuthorize("@ss.hasPermi('ktmall:category:add')")
    @Log(title = "分类菜单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GoodsCategory goodsCategory)
    {
        return toAjax(goodsCategoryService.save(goodsCategory));
    }

    /**
     * 修改分类菜单
     */
    @PreAuthorize("@ss.hasPermi('ktmall:category:edit')")
    @Log(title = "分类菜单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GoodsCategory goodsCategory)
    {
        return toAjax(goodsCategoryService.updateById(goodsCategory));
    }

    /**
     * 删除分类菜单
     */
    @PreAuthorize("@ss.hasPermi('ktmall:category:remove')")
    @Log(title = "分类菜单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Integer> ids)
    {
        return toAjax(goodsCategoryService.removeByIds(ids));
    }
}